/**
 * 
 */
package net.looker.monitor.schdule;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;

import net.looker.config.ConfigManager;
import net.looker.config.SiteConfig;
import net.looker.data.Category;
import net.looker.data.News;
import net.looker.monitor.analyzer.CategoryAnalyzer;
import net.looker.monitor.checker.AddChecker;
import net.looker.monitor.checker.Checker;
import net.looker.monitor.checker.DeleteChecker;
import net.looker.monitor.crawler.CrawlerException;
import net.looker.monitor.crawler.WebCrawler;

/**
 * 栏目监视器
 * @author solosky
 *
 */
public class CategoryMonitor extends Monitor
{
	/**
	 * 栏目新闻管理器
	 */
	private  NewsManager newsManager;
	
	/**
	 * 站点配置
	 */
	private SiteConfig siteConfig;
	/**
	 * 栏目信息
	 */
	private Category category;
	
	/**
	 * 日志记录对象
	 */
	private  Logger logger;
	
	
	/**
	 * 检查器
	 */
	private ArrayList<Checker> checkerList;
	
	public CategoryMonitor(Category cate)
	{
		super(cate.getTitle()+"-监视器",cate.getCategoryHash(),cate.getMonitorInterval());
		category    = cate;
		siteConfig  = cate.getSiteConfig();
		newsManager = new NewsManager();
		checkerList = new ArrayList<Checker>();
		checkerList.add(new AddChecker(newsManager));
		//checkerList.add(new DeleteChecker(newsManager));
		logger      = Logger.getLogger("looker");
		
	}

	/* (non-Javadoc)
	 * @see net.looker.monitor.Monitor#startMonitor()
	 */
	@Override
	public void startMonitor()
	{
		if (status==Monitor.STATUS.RUN)
		{
			logger.info("栏目监视器:"+category.getTitle()+"开始检查");
			this.action = ACTION.MONITOR;
			crawlList();
			if(newsManager.isFirstCheck())
			{
				newsManager.update();
				return;
			}
			newsManager.buildHashMap();
			checkUpdate();
			newsManager.update();
			this.action = ACTION.WAITING;
			Logger.getLogger("looker").info("栏目监视器:"+category.getTitle()+"完成检查");
		}
	}
	
	/**
	 * 获取新闻列表
	 */
	public void crawlList()
	{
		
		try {
			//抓取新闻列表页
			WebCrawler crawler = new WebCrawler(category.getURL(1),siteConfig.get("site.charset"));
			crawler.crawl();
			String html = (String) crawler.getCrawled();
			
			//分析出列表项目
			CategoryAnalyzer categoryAnalyzer = new CategoryAnalyzer(html, category);
			categoryAnalyzer.analyze();
			//保存当前分析的新闻列表
			newsManager.setCurNewsList((ArrayList<News>)categoryAnalyzer.getAnalyzed());
			logger.info("栏目监视器:共分析出新闻 "+newsManager.getCurNewsList().size()+"条-"+category.getIndex());
			} catch (MalformedURLException e) {
			logger.warning("栏目监视器："+e.getMessage());
		} catch (IOException e) {
			logger.warning("栏目监视器："+e.getMessage());
		} catch (CrawlerException e) {
			logger.warning("栏目监视器："+e.getMessage());
		}
	}

	/**
	 * 检查新闻变化
	 */
	public void checkUpdate()
	{
		Iterator <Checker> it = checkerList.iterator();
		while (it.hasNext())
			it.next().check();
	}
	

	//test////
	public static void main(String[] args) throws InterruptedException
	{
		

	}



}
